package com.datasecops.scannerservices.db;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.datasecops.scannerservices.enumerate.DBDrive;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 达梦数据库相关操作
 * @author: GeXiaoLong
 * @time: 2022/10/10 18:54
 */
public class DM extends DBHelper{
    private static DM instance = null;

    public static Map<String, DruidDataSource> dataSourceMap = new HashMap<>();

    public static DM getInstance() {
        if (null == instance) {
            synchronized (Mysql.class) {
                if (null == instance) {
                    instance = new DM();
                }
            }
        }
        return instance;
    }

    public static DM getInstance(String ip, int port, String user, String password,String Instance) {
        //初始化数据源
        if (null == instance) {
            synchronized (DM.class) {
                if (null == instance) {
                    instance = new DM();
                }
            }
        }

        //如果map没有这个数据源，则新建
        if (!DM.getInstance().containsId(getSourceMapKey(ip, port,Instance))) {
            DM.getInstance().addDataSource(ip, port, user, password,Instance);
        }

        //初始化数据源
        return instance;
    }

    /**
     * 获取连接
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 13:57
     */
    @Override
    public Connection getConnection(String key) {
        Connection conn = null;
        try {
            DruidDataSource source = dataSourceMap.get(key);
            conn = source.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }




    /**
     * 插入数据源
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:13
     */
    @Override
    public void addDataSource(String ip, int port, String user, String password,String Instance) {
        String url = "jdbc:dm://" + ip + ":" + port+"/"+Instance;
        //+Instance
        //+"/"+Instance
        //String properties = "useUnicode=true;characterEncoding=UTF8;useSSL=true;serverTimezone=Asia/Shanghai";
        Properties prop = getCommonProp(user,password, DBDrive.DM.getValue(),url,null);
        try {
            DruidDataSource druidDataSource = (DruidDataSource) DruidDataSourceFactory
                    .createDataSource(prop);

            putSourceMap(getSourceMapKey(ip, port,Instance), druidDataSource);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("初始化创建连接池失败！");
        }
    }

    /**
     * 判断数据源是否存在
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:09
     * @param
     * @return
     */
    @Override
    public boolean containsId(String id) {
        return dataSourceMap.containsKey(id);
    }

    /**
     * putMap
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:23
     * @param
     * @return
     */
    @Override
    public void putSourceMap(String sourceMapKey, DruidDataSource druidDataSource) {
        dataSourceMap.put(sourceMapKey,druidDataSource);
    }

    /**
     * removeMap
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:23
     * @param
     * @return
     */
    @Override
    public void removeSourceMap(String sourceMapKey) {
        dataSourceMap.remove(sourceMapKey);
    }

}
